define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

  var Controller = {
    index: function () {
      // 初始化表格参数配置
      Table.api.init({
        extend: {
          index_url: 'company/index' + location.search,
          add_url: 'company/add',
          edit_url: 'company/edit',
          del_url: 'company/del',
          multi_url: 'company/multi',
          // import_url: 'company/import',
          table: 'company',
        }
      });

      var table = $("#table");

      // 初始化表格
      table.bootstrapTable({
        url: $.fn.bootstrapTable.defaults.extend.index_url,
        pk: 'id',
        sortName: 'id',
        fixedColumns: true,
        fixedRightNumber: 1,
        columns: [
          [
            {checkbox: true},
            {field: 'id', title: __('Id')},
            {field: 'name', title: __('Name'), operate: 'LIKE'},
            {field: 'code', title: __('Code'), operate: 'LIKE'},
            {
              field: 'license',
              title: '营业执照',
              operate: false,
              events: Table.api.events.image,
              formatter: Table.api.formatter.image
            },
            {field: 'address', title: '企业地址', operate: 'LIKE'},
            {field: 'leader_name', title: '法人名称', operate: 'LIKE'},
            {field: 'leader_phone', title: '法人电话', operate: 'LIKE'},
            {field: 'leader_email', title: '邮箱', operate: 'LIKE'},
            {field: 'create_time', title: '认证时间', operate: 'RANGE', addclass: 'datetimerange', autocomplete: false},
            {field: 'user.nickname', title: '邮箱', operate: 'LIKE'},
            {field: 'user_id', title: '用户ID', operate: 'LIKE'},
            {field: 'user.nickname', title: '昵称', operate: 'LIKE'},
            {field: 'user.username', title: '用户名', operate: 'LIKE'},
            {field: 'user.mobile', title: '注册手机号', operate: 'LIKE'},
            {field: 'user.createtime', title: '注册时间', operate: 'RANGE', addclass: 'datetimerange'},
            {
              field: 'operate',
              title: __('Operate'),
              table: table,
              events: Table.api.events.operate,
              formatter: Table.api.formatter.operate
            }
          ]
        ]
      });

      // 为表格绑定事件
      Table.api.bindevent(table);
    },
    add: function () {
      Controller.api.bindevent();
    },
    edit: function () {
      Controller.api.bindevent();
    },
    detail: function () {
      Controller.api.bindevent();
    },
    api: {
      bindevent: function () {
        Form.api.bindevent($("form[role=form]"));
      }
    }
  };
  return Controller;
});
